<?php
class ProductController extends FrontendController
{
	public $layout="column2";
	public function actionIndex()
	{
		$totalProduct = FrontendShopProductsModel::getTotalProductFeature();
		$page = new CPagination($totalProduct);
		$itemOnPaging = 5;
		$page->pageSize = Yii::app()->params['list_product_cat'];
		$curr_page = $page->getCurrentPage();
		$bestSale = FrontendShopProductsModel::model()->findAll('featured=1 AND published=1');
		$this->render('index', array(
				'total'=>$totalProduct,
				'data'=>$bestSale,
				'page'=>$page,
				'curr_page'=>$curr_page,
				'itemOnPaging'=>$itemOnPaging
		));
	}
	public function actionDetail()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseurl.'/js/jquery-ui.js');
		$this->layout='main';
		$id = Yii::app()->request->getParam('id','');
		$data = FrontendShopProductsModel::getDetail($id);
		$categoryId = FrontendShopProductCategoryModel::model()->getCategoryId($id);
		$aa = array();
		$cat = FrontendShopCategoryModel::getAllCategoryToRoot($categoryId, $aa);
		$rootCat = $aa[count($aa)-1];
		$nameCat = FrontendShopCategoryModel::model()->findByPk($rootCat)->title;
		//add view
		$model = FrontendShopProductsModel::model()->updateViewd($id);
		//get releat product
		$releatProducts = FrontendShopProductsModel::model()->getProductReleat($data['category_id'], $id);
		$this->render('detail', array(
					'data'=>$data,
					'releatProducts'=>$releatProducts,
					'rootCat'=>$rootCat
				));
	}
	public function actionCategory()
	{
		$this->layout='column4';
		$catId = Yii::app()->request->getParam('id','');
		$category = FrontendShopCategoryModel::model()->findByPk($catId);
		$catName = $category->title;
		$thumbImage = $category->image;
		$totalProduct = FrontendShopProductsModel::getTotalProductByCat($catId);
		$page = new CPagination($totalProduct);
		$itemOnPaging = 5;
		$page->pageSize = Yii::app()->params['list_product_cat'];
		$curr_page = $page->getCurrentPage();
		
		$data = FrontendShopProductsModel::getProductByCat($catId, $page->getLimit(), $page->getOffset());
		$this->render('category', array(
				'catID'=>$catId,
				'catName'=>$catName,
				'thumbImage'=>$thumbImage,
				'total'=>$totalProduct,
				'data'=>$data,
				'page'=>$page,
				'curr_page'=>$curr_page,
				'itemOnPaging'=>$itemOnPaging
		));
	}
	public function actionSearch()
	{
		$category = isset($_GET['category'])?$_GET['category']:6;
		$manufactor = isset($_GET['manufactor'])?$_GET['manufactor']:'';
		$price = isset($_GET['price'])?$_GET['price']:'';
		$keyword = isset($_GET['keyword'])?$_GET['keyword']:'';
		
		$order = "lastest";
		$orderQuery="";
		if(isset($_POST['Products'])){
			$order = $_POST['Products']['order'];
			switch ($order){
				case 'hight':
					$orderQuery = "ORDER BY price DESC";
					break;
				case 'low':
					$orderQuery = "ORDER BY price ASC";
					break;
				default:
					$orderQuery = "";
					break;
			}
		
		}
		
		$total = FrontendShopProductsModel::getTotalProductBySearch($category, $keyword, $price);
		$page = new CPagination($total);
		$itemOnPaging = 5;
		$page->pageSize = Yii::app()->params['perpage'];
		$curr_page = $page->getCurrentPage();
		
		
		$data = FrontendShopProductsModel::getProductBySearch($category, $keyword, $price, $page->getLimit(), $page->getOffset(), $orderQuery);
		
		$this->render('search', array(
					'catID'=>$category,
					'total'=>$total,
					'data'=>$data,
					'page'=>$page,
					'curr_page'=>$curr_page,
					'order'=>$order
				));
	}
}